package com.his.finance.service.impl;

import com.his.finance.dao.FinInvoiceStoreMapper;
import com.his.finance.entity.FinInvoiceStore;
import com.his.finance.service.FinInvoiceStoreService;
import com.his.finance.vo.FinInvoiceStoreVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.exception.HisExecption;
import com.xiao.common.util.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class FinInvoiceStoreImpl implements FinInvoiceStoreService {
    @Resource
    private FinInvoiceStoreMapper fineInvoiceStoreMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertFinInvoiceStore(DictEmployee employee, FinInvoiceStoreVo finInvoiceStoreVo) throws Exception{
        //接收前端数据
        FinInvoiceStore fineInvoiceStore = BeanUtil.copyBean(finInvoiceStoreVo,FinInvoiceStore.class);
        //查询当前操作员有无票据号
        FinInvoiceStore finInvoiceStore = fineInvoiceStoreMapper.voiceStore_select("060105",employee.getEMPID());
        if(finInvoiceStore != null){
            //有票据号 更新票据
            fineInvoiceStore.setInvempid(employee.getEMPID());
            fineInvoiceStore.setInvtype("060105");
            fineInvoiceStoreMapper.voiceStore_update(fineInvoiceStore);
        }else{
            //插入票据
            //计算发票数
            int count = 0;
            String str = finInvoiceStoreVo.getInvstr();
            String strs[] =str.split("-");
            count = Integer.parseInt(strs[1])-Integer.parseInt(strs[0])+1;

            //第一次创建当前操作员收据  验证票据一致性
            if(count != fineInvoiceStore.getInvcount()){
                throw HisExecption.serverErrException("保存失败，发票总数不一致！");
            }
            if(strs[1].equals(fineInvoiceStore.getInvnow())){
                throw HisExecption.serverErrException("保存失败，发票起始号不一致！");
            }
            fineInvoiceStore.setInvempid(employee.getEMPID());
            fineInvoiceStore.setInvtype("060105");
            fineInvoiceStoreMapper.voiceStore_add(fineInvoiceStore);
        }
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public FinInvoiceStore selectFinInvoiceStoreNum(DictEmployee employee) {
        return fineInvoiceStoreMapper.voiceStore_select("060105",employee.getEMPID());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFinInvoiceStore(DictEmployee employee,FinInvoiceStoreVo finInvoiceStoreVo) {
        FinInvoiceStore finInvoiceStore =selectFinInvoiceStoreNum(employee);

        if(finInvoiceStore.getInvcount() == 1){
            finInvoiceStore.setInvcount(0);
            finInvoiceStore.setInvnow("0");
        }else{
            int invcount = finInvoiceStore.getInvcount()-1;            ;
            String invnow = String.valueOf(Integer.parseInt(finInvoiceStore.getInvnow())+1);
            finInvoiceStore.setInvcount(invcount);
            finInvoiceStore.setInvnow(invnow);
        }
        fineInvoiceStoreMapper.voiceStore_update(finInvoiceStore);
    }
}
